<?php
namespace App\Services\Adv\Adv;

use App\Models\Adv\AdvClassify;
use App\Services\Adv\ValidateAdvClassify;
use App\Services\Manage\ManageIndex;
use Illuminate\Support\Facades\DB;
/**
 * 广告位类
 * @author  ws
 * 验证：
        添加或编辑时：
        [不需要验证的字段:''默认值]   [需要验证但不require的:' '默认,使其验证]   [必须require的字段:无需默认值]
        搜索时：
        可空，给予''默认值，跳过验证器即搜索值为空时，不验证，不搜索
 * 编辑类，传什么验什么
 */
class AdvClassifyLogic
{
    /*根据id获取其父类的ids数组*/
    public function getParentIds($id): array 
    {
        $list = AdvClassify::where('status', '<>', -1)->get();
        $ids     = $id;
        $params  = 'id';
        $parentIds = parent_ids($list,$ids,$params,$id,0,'pid','id','');
        $ids = explode(",", $ids);
        foreach ($ids as $k => &$v) {
            $v = (int)$v;
        }
        array_splice($ids, array_search(0, $ids), 1);
        return array_reverse($ids);
    }
     // 广告位分类添加
    public function advClassifyAdd($data)
    {
       //获得总后台的APP_id
        $manageAppId = ManageIndex::Api()->getAppId();
        if ($manageAppId != '11') {
            if ($manageAppId != $data['app_id']) {
                return auto_failure ($data,'该分类不属于本应用，无权限添加子分类！');
            }
        }
        if ($data['is_public'] == 1 && $data['pid'] != 0) {
            $checkPublic = $this->checkIsPublic($data['pid']);
            if ($checkPublic === false) {
                return auto_failure ($data,'该分类的父级为私有分类，私有分类下不可创建公共分类！');
            }
        }
        $data['create_time'] = time();
        $data['update_time'] = time();
        $rst = AdvClassify::insertGetId($data);
        if ($rst === false) {
            return auto_failure ($data,'广告位分类添加失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位分类未作改动！');
        }
        return auto_return(['id' => $rst],'分类添加成功！');
    }
    // 检验父类是否为公共分类
    public function checkIsPublic($pid)
    {
        $res = AdvClassify::where([['status', '<>', -1],['is_public', '=', 1],['id', '=', $pid]])->first();
        if ($res === null) {
            return false;
        }
        return true;
    }
     // 获取某应用全部广告位分类列表 树状
    public function advClassifyList($data)
    {
       //获得总后台的APP_id
        $manageAppId = ManageIndex::Api()->getAppId();
        if($manageAppId == 11){
             $arr  = AdvClassify::where('status', '<>', -1)
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();
        }else{
             $arr  = AdvClassify::where('status', '<>', -1)
              ->where(function ($query) use ($manageAppId) {
                $query->orWhere('app_id', '=', intval($manageAppId))
                      ->orWhere('is_public', '=', 1)
                      ->orWhereRaw ("FIND_IN_SET($manageAppId,other_app_read)");
               })
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();
        }
        $list = list_to_tree($arr,'id','pid','_child', 0);
        return auto_return($list);
    }
     // 某条广告位分类信息编辑修改
    public function advClassifyEdit($data)
    {
        $cateInfo = $this->dbCateInfo($data);
        if ($data['is_public'] == 1 && $data['pid'] != 0) {
            $checkPublic = $this->checkIsPublic($data['pid']);
            if ($checkPublic === false) {
                return auto_failure ($data,'该分类的父级为私有分类，私有分类下不可创建公共分类！');
            }
        }
        $data['update_time'] = time();
        $rst = AdvClassify::where('id',$data['id'])->update($data);
        if ($rst === false) {
            return auto_failure ($data,'分类信息更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类信息未作改动！');
        }
        if ($data['pid'] == 0) {
            if ($data['other_app_read'] != $cateInfo['other_app_read']) {
                $res = AdvClassify::where('app_id',$data['app_id'])->update(['other_app_read' => $data['other_app_read']]);
                if ($res === false) {
                    return auto_failure ($data,'子分类可读应用信息更改失败！');
                } elseif ($rst === 0) {
                    return auto_failure ($data,'子分类可读应用信息未作改动！');
                }
            }
        }

        return auto_return(['number' => $rst],'分类信息更改成功！');
    }
    //内部获取某个分类的详情
    public function dbCateInfo($data)
    {
        return AdvClassify::where([['id', '=', $data['id']],['status','<>',-1]])->first();
    }

    /*检验识别码的唯一性*/
    public function checkAction($data): array
    {
        $info = AdvClassify::where(['action'=>$data['action']])->first();
        if($info){
            return auto_failure(null, '该识别码action已存在');
        }else{
            return auto_success(null, '识别码有效');
        }
    }

     // 启用某个分类广告位分类
    public function advClassifyEnable($data): array
    {
        $updateData['status'] = 1;
        $updateData['update_time'] = time();
        $rst = AdvClassify::where('id',$data['id'])->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'分类状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类状态未发生修改！');
        }
        return auto_return(['number' => $rst], '分类状态修改成功！');
    }
    // 禁用某个分类广告位分类
    public function advClassifyDisable($data): array
    {
        $updateData['status'] = 0;
        $updateData['update_time'] = time();
        $rst = AdvClassify::where('id',$data['id'])->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'分类状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类状态未发生修改！');
        }
        return auto_return(['number' => $rst], '分类状态修改成功！');
    }
     // 删除某个分类广告位分类
    public function advClassifyDelete($data): array
    {
        $updateData['status'] = -1;
        $updateData['update_time'] = time();
        $rst = AdvClassify::where('id',$data['id'])->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'分类状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类状态未发生修改！');
        }
        return auto_return(['number' => $rst], '分类状态修改成功！');
    }
     // 获取某个广告位分类 非删除
    public function advClassifySomeList($data): array
    {
        $res = AdvClassify::where([['id', '=',$data['pid']],['app_id','=',$data['app_id']]])->first();
        if (empty($res)) {
            return null_data($res,"分类的id和标识不匹配，请重试！");
        }
        $arr = AdvClassify::where([['status', '=',1],['pid','=',$data['pid']]])->orderBy('id', 'desc')->get()->toArray();
        $list = mosaic_child($arr);
        return auto_return($list);
    }
    // 不同条件广告位分类列表获取 非删除 分类名称 描述
    public function advClassifyCondition($data): array
    {   
        $keywords = $data['keywords'];
        $arr = AdvClassify::where('status','=',1)
        ->where(function($query) use ($keywords){
        $query->where('name','like',"%{$keywords}%")
        ->orWhere('description', 'like',"%{$keywords}%");
        })          
        ->orderBy('id', 'desc')
        ->paginate($data['listRows'], null, null, $data['page'])
        ->toArray();
        return auto_return_page($arr);
    }
     // 获取某一广告位分类详情
    public function classifyInfo($data): array
    {
        $res = AdvClassify::where('id',$data['id'])->first();
        if ($res == null) {
            return auto_failure($res,"未找到该分类，请检查id！");
        }
        return auto_return($res);
    }


     
}
